/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.eit.ntc.lmsbackend.validator;

import com.eit.ntc.lmsbackend.struts.form.CatalogForm;
import java.util.Locale;
import java.util.ResourceBundle;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 *
 * @author tawatchai
 */
public class CatalogValidator implements Validator{
    private static Locale locale = new Locale("th","TH");

    /**
     * @return the locale
     */
    public static Locale getLocale() {
        return locale;
    }

    /**
     * @param aLocale the locale to set
     */
    public static void setLocale(Locale aLocale) {
        locale = aLocale;
    }

    
    private ResourceBundleMessageSource resource;

    //private static ResourceBundle resource1 = ResourceBundle.getBundle("com/eit/ntc/lmsbackend/struts/ApplicationResource");

    public boolean supports(Class clazz) {
        return CatalogForm.class.equals(clazz);
    }

    public void validate(Object arg0, Errors errors) {
        CatalogForm cForm = (CatalogForm) arg0;
        String[] catalogNameArg = getErrorArgument("CatalogForm.field.catalogName");
        String[] catalogDescArg = getErrorArgument("CatalogForm.field.catalogDesc");

         ValidationUtils.rejectIfEmptyOrWhitespace(errors, "catalogName","errors.required" ,catalogNameArg, "catalog-name must not be empty");
         ValidationUtils.rejectIfEmptyOrWhitespace(errors, "catalogDesc","errors.required" ,catalogDescArg, "catalog-desc must not be empty");
         //ValidationUtils.
    }

    public void validateAddItem(Object arg0, Errors errors) {
        CatalogForm cForm = (CatalogForm) arg0;
        
         //ValidationUtils.
    }

    /**
     * @return the resource
     */
    public ResourceBundleMessageSource getResource() {
        return resource;
    }

    /**
     * @param resource the resource to set
     */
    public void setResource(ResourceBundleMessageSource resource) {
        this.resource = resource;
    }

    private String[] getErrorArgument(String argName) {
       return new String[] {resource.getMessage(new DefaultMessageSourceResolvable(argName), locale)};
    }

}
